package client.view;


import java.awt.AWTException;
import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.IOException;
import java.net.UnknownHostException;
import java.util.Timer;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import client.main.Client;
import client.main.Writer;
import client.mobile.Player;
import client.utility.PacketId.PacketID;
import client.view.keys.KeyTimer;



public class Gui implements ActionListener, KeyListener, WindowListener {
	
	public static void main(String[] args) throws UnknownHostException, IOException, AWTException{
		new Gui();
	}
	
	private JFrame frame;
	private JComponent center;
	private Writer writer;
	private Player player;
	private JTextField text;
	private JButton create;
	private JButton ok;
	private KeyTimer keyTimer;
	//private long nextUpdate;
	
	public void updateData(){
		center.repaint();
		
		frame.repaint();
		frame.validate();
	}
	
	public Gui ()
	{
		frame=new JFrame("RPG game");
		frame.setSize(800, 600);
		
		keyTimer=new KeyTimer();
		
		center=getLoginPanel();
		
		frame.getContentPane().setLayout(new BorderLayout());
		
		frame.getContentPane().add(center,"Center");
		
		frame.repaint();
		frame.validate();
		
		frame.addWindowListener(this);
		frame.setResizable(false);
		frame.setVisible(true);
		
		Timer askerTimer = new Timer("key timer", false);
		askerTimer.scheduleAtFixedRate(keyTimer, 150, 150);
	}
	
	private JComponent getLoginPanel() {
		JPanel p=new JPanel();
		p.setLayout(new GridBagLayout());
		GridBagConstraints c=new GridBagConstraints();
		c.gridx=0;
		c.gridy=0;
		
		
		p.add(new JLabel("Insert the name of the pg"),c);
		c.gridx++;
		
		text=new JTextField(30);
		p.add(text,c);
		
		
		c.gridx=0;
		c.gridy++;
		ok=new JButton("Ok");
		ok.addActionListener(this);
		p.add(ok,c);
		
		c.gridx++;
		create=new JButton("Create");
		create.addActionListener(this);
		p.add(create,c);
		
		return p;
	}

	private void login(){
		frame.getContentPane().remove(center);
		center=new ViewPort(player);
		
		frame.getContentPane().add(center,"Center");
		
		//center.setFocusable(true);
		frame.requestFocus();
		frame.addKeyListener(this);
		
		this.updateData();
	}
	
	public JFrame getFrame(){
		return frame;
	}

	public void actionPerformed(ActionEvent e) {
		String userName=text.getText();
		
		try {
			boolean done=false;
			
			if(e.getSource()==ok)
				done=Client.loginRequest(userName, this,PacketID.login);
			else
				done=Client.loginRequest(userName, this,PacketID.createPg);
			
			if(done)
				login();
		} catch (Exception ex) {}
	}

	public void setWriter(Writer writer) {
		this.writer=writer;
		this.keyTimer.setWriter(writer);
	}

	public void setPlayer(Player player) {
		this.player=player;
		this.keyTimer.setPlayerName(player.getName());
	}

	public void keyPressed(KeyEvent e){
		//long now=new Date().getTime();
		
		//if(now<nextUpdate)
			//return;
		
		//nextUpdate=now+50;
		
		//String code=keys.getCode(e.getKeyCode());
		
		this.keyTimer.add(e.getKeyCode());
		
		/*switch(e.getKeyCode()){
			case KeyEvent.VK_DOWN: code="down"; break;
			case KeyEvent.VK_UP: code="up"; break;
			
			case KeyEvent.VK_LEFT: code="left"; break;
			case KeyEvent.VK_RIGHT: code="right"; break;
		}*/
		
		//if(code==null)
			//return;
		
		//try {
			//writer.write(PacketID.userAction,code+";"+player.getName());
		//} catch (IOException e1) {}
	}

	@Override
	public void keyReleased(KeyEvent e) {
		this.keyTimer.remove(e.getKeyCode());
	}

	@Override
	public void keyTyped(KeyEvent e) {}

	@Override
	public void windowActivated(WindowEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowClosed(WindowEvent e) {
		
	}

	@Override
	public void windowClosing(WindowEvent e) {
		if(center instanceof ViewPort){
			try {
				writer.write(PacketID.logout, player.getName());
			} catch (IOException e1) {}
		}
		
		System.exit(0);
	}

	@Override
	public void windowDeactivated(WindowEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowDeiconified(WindowEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowIconified(WindowEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowOpened(WindowEvent e) {
		// TODO Auto-generated method stub
		
	}
}